//
//  Reset_login_pwd_vc.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/3.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Reset_login_pwd_vc.h"
#import "JKCountDownButton.h"

@interface Reset_login_pwd_vc ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *codeTf;
@property (weak, nonatomic) IBOutlet JKCountDownButton *get_checkCode_btn;
@property (weak, nonatomic) IBOutlet UITextField *pwd_tf;

@end

@implementation Reset_login_pwd_vc
- (IBAction)pwd_tf_see_action:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.pwd_tf.secureTextEntry = !self.pwd_tf.secureTextEntry;
}
- (IBAction)get_code_action:(JKCountDownButton *)sender {
    if(self.phoneTf.text.length != 11){
        [MBProgressHUD showTipMessageInView:@"请输入11位手机号"];
        return;
    }
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:[Anktool strBlankHolder:self.phoneTf.text] forKey:@"mobile"];
    [dict setObject:app_code forKey:@"app_code"];
    [dict setObject:channel_type forKey:@"channel_type"];
    
    
    
    [PPHTTPRequest req:@"api/Auth/sms" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            [MBProgressHUD showTipMessageInView:@"发送成功"];
            [self.codeTf becomeFirstResponder];
            sender.enabled = NO;
            //button type要 设置成custom 否则会闪动
            [sender startCountDownWithSecond:60];
            
            [sender countDownChanging:^NSString *(JKCountDownButton *countDownButton,NSUInteger second) {
                NSString *title = [NSString stringWithFormat:@"剩余%zd秒",second];
                return title;
            }];
            [sender countDownFinished:^NSString *(JKCountDownButton *countDownButton, NSUInteger second) {
                countDownButton.enabled = YES;
                return @"点击重新获取";
                
            }];
            
            
        }
        else
//            if ([error_code isEqualToString:@""])
            {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        
    }];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.get_checkCode_btn.layer.borderWidth = 1;
    self.get_checkCode_btn.layer.borderColor = themeColor.CGColor;
    self.phoneTf.delegate = self.codeTf.delegate = self.pwd_tf.delegate = self;
    // Do any additional setup after loading the view from its nib.
    // /api/Auth/sms
    
    //mobile=18162152664&channel_type=android&app_code=ainankang
    /*
     
     {
     "error_code":0,
     "error_msg":"操作成功",
     "data":[
     
     ]
     }
     */
    
    // /api/Auth/resetPassword
    
    // password=123456&mobile=18162152664&verification=432170&app_code=ainankang
    
    
    /*
     {
     "error_code":0,
     "error_msg":"操作成功",
     "data":[
     
     ]
     }
     */
    
}

- (IBAction)confirmAction:(UIButton *)sender {
    if(self.phoneTf.text.length != 11){
        [MBProgressHUD showTipMessageInView:@"请输入11位手机号"];
        return;
    }
    if (self.codeTf.text.length != 6) {
        [MBProgressHUD showTipMessageInView:@"请输入6位数验证码"];
        return;
    }
    if(self.pwd_tf.text.length < 6 ){
        [MBProgressHUD showTipMessageInView:@"请输入6-16位字母数字组合"];
        return;
    }

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.pwd_tf.text forKey:@"password"];
    [dict setObject:self.phoneTf.text forKey:@"mobile"];
    [dict setObject:self.codeTf.text forKey:@"verification"];
    [dict setObject:app_code forKey:@"app_code"];
    
    sender.userInteractionEnabled = NO;
    [PPHTTPRequest req:@"api/Auth/resetPassword" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            
            [self.navigationController popToRootViewControllerAnimated:YES];
            [MBProgressHUD showTipMessageInWindow:@"已重置，请登录"];
        }
        else{
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
        sender.userInteractionEnabled = YES;
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        sender.userInteractionEnabled = YES;
    }];
    
}



#pragma mark - ================== tf delegate ==================
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSMutableString *newtxt = [NSMutableString stringWithString:textField.text];
    
    
    [newtxt replaceCharactersInRange:range withString:string];
    
    // 客户端仅仅判断手机号是否11位
    if ([textField isEqual:self.phoneTf]) {// 手机号11位
        int MAX_CHARS = 11;
        
        return ([newtxt length] <= MAX_CHARS);
        
    }else if([textField isEqual:self.codeTf]){
        
            return ([newtxt length] <= 6);
       
        
    }else if([textField isEqual:self.pwd_tf]){
        
        return ([newtxt length] <= 16 && [Anktool is_num_alpha:newtxt]);
        
        
    }
    
    
    return YES;
    
}

@end
